package cn.maidouya.product.service.impl;

import cn.maidouya.basic.exception.BusinessExcepiton;
import cn.maidouya.basic.service.impl.BaseServiceImpl;
import cn.maidouya.product.domain.Product;
import cn.maidouya.product.domain.ProductDetail;
import cn.maidouya.product.mapper.ProductDetailMapper;
import cn.maidouya.product.mapper.ProductMapper;
import cn.maidouya.product.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ProductService extends BaseServiceImpl<Product> implements IProductService {
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private ProductDetailMapper productDetailMapper;
    @Override
    @Transactional
    public void delete(Long productId) {
        // 1. 先删除关联表(详情页)
        productDetailMapper.deleteByProductId(productId);
        // 2. 先删除主表(服务)
        productMapper.delete(productId);
    }

    /**
     *    添加服务和详情页
     * @param product
     */
    @Override
    @Transactional
    public void save(Product product) {
        productMapper.save(product);
        ProductDetail detail = product.getDetail();
        if (detail != null){
            detail.setProduct_id(product.getId());
            productDetailMapper.save(detail);
        }
    }

    @Override
    @Transactional
    public void update(Product product) {
        productMapper.update(product);
        ProductDetail detail = product.getDetail();
        if (detail.getId() != null){
            detail.setProduct_id(product.getId());
                // 这个时详情页，因为添加服务时候没有添加详情页，这时候详情页可存在空
                if (detail.getId() != null){
                    productDetailMapper.update(detail);
                }else {
                    productDetailMapper.save(detail);
                }

        }
    }

    @Override
    public ProductDetail loadDetailByProductId(Long productId) {
        System.err.println("查询详情页" +productId);
        return productDetailMapper.loadDetailByProductId(productId);
    }

    /**
     *   批量上架和批量下架
     * @param ids
     * @param flag  1上架 0下架
     */
    @Override
    public void onOROffSale(List<Long> ids, int flag) {
        Map<String, Object> map = new HashMap<>();
        if (flag == 1){ // 批量上架
            map.put("onsaletime", new Date());
            map.put("ids",ids);
            productMapper.onSale(map);
        }else {// 批量下架
            map.put("offsaletime", new Date());
            map.put("ids",ids);
            productMapper.offSale(map);
        }
    }
   // 批量删除
    @Override
    public void batchDelete(List<Long> ids) {
        // 前端做了选择较空
        Map<String, Object> map = new HashMap<>();
            map.put("ids",ids);
            // 1. 先删除关联表(详情页)
            productDetailMapper.batchDeleteByProductids(map);
            // 2. 先删除主表(服务)
            productMapper.batchDeleteIds(map);

    }
    // 右侧经典搭配查询
    @Override
    public List<Product> loadMatch() {
        List<Product> list = productMapper.loadMatch();
        return list;
    }


}